#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define pb push_back
#define mp make_pair
long long t;
long long n;
long long a[200020];
long long paths[21];
long long p[21];
long long z;
long long zz;
long long check1()
{
    long long re = 0;
    // for (long long i = 1; i <= n; i++)
    // {
    //     cout << paths[i] << " ";
    // }
    // cout << "\n";
    for (long long i = 1; i <= n; i++)
    {
        p[i] = paths[a[i]];
        // cout << p[i] << " ";
    }
    // cout << "\n";
    for (long long i = 1; i <= n; i++)
    {
        for (long long j = i - 1; j >= 1; j--)
        {
            if (p[i] == p[j] && a[i] == a[j])
            {
                re += a[i];
            }
            if (p[i] == p[j])
                break;
        }
    }
    // cout << re << "\n";
    return re;
}
void dfs(long long dep)
{
    if (dep > zz)
    {
        z = max(z, check1());
        return;
    }
    dfs(dep + 1);
    paths[dep] = 1;
    dfs(dep + 1);
    paths[dep] = 0;
}
long long check2()
{
    long long re = 0;
    // for (long long i = 1; i <= n; i++)
    // {
    //     cout << paths[i] << " ";
    // }
    // cout << "\n";
    // for (long long i = 1; i <= n; i++)
    // {
    //     p[i] = paths[a[i]];
    //     // cout << p[i] << " ";
    // }
    // cout << "\n";
    for (long long i = 1; i <= n; i++)
    {
        for (long long j = i - 1; j >= 1; j--)
        {
            if (paths[i] == paths[j] && a[i] == a[j])
            {
                re += a[i];
            }
            if (paths[i] == paths[j])
                break;
        }
    }
    // cout << re << "\n";
    return re;
}
void work1()
{
    memset(paths, 0, sizeof paths);
    z = 0;
    dfs(1);
    cout << z << "\n";
}
void dfs2(long long dep)
{
    if (dep > n)
    {
        z = max(z, check2());
        return;
    }
    dfs2(dep + 1);
    paths[dep] = 1;
    dfs2(dep + 1);
    paths[dep] = 0;
}
void work2()
{
    memset(paths, 0, sizeof paths);
    z = 0;
    dfs2(1);
    cout << z << "\n";
}
signed main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    cin >> t;
    while (t--)
    {
        cin >> n;
        // long long ff = 0;
        zz = 0;
        for (long long i = 1; i <= n; i++)
        {
            cin >> a[i];
            // if (a[i] > 15)
            // ff = 1;
            zz = max(zz, a[i]);
            // f[i][0] = f[i][1] = 0;
        }
        if (n <= 15)
        {
            work2();
            continue;
        }

        work1();
        // continue;
    }
    return 0;
}
